<?php
/*
User Email System
Version 1.0
by:vbgamer45
http://www.smfhacks.com
*/

if (!defined('SMF'))
	die('Hacking attempt...');

function UserEmailMain()
{
	global $context, $mbname, $webmaster_email, $ID_MEMBER, $txt, $db_prefix,$sourcedir;

	//Check if the current user can send emails
	isAllowedTo('send_useremail');

	if(isset($_GET['sa']))
	{

		if($_GET['sa'] == 'save')
		{

			$subject = $_POST['subject'];
			if($subject == '')
				fatal_error('You need to enter a subject.',false);
			$message = $_POST['message'];
			if($message == '')
				fatal_error('You need to enter a message.',false);


			$userid = $_POST['userid'];

	$request = db_query("SELECT memberName, emailAddress FROM {$db_prefix}members WHERE ID_MEMBER = $userid LIMIT 1", __FILE__, __LINE__);
	$row = mysql_fetch_assoc($request);

	$rec = $row['memberName'];
	$rec_email = $row['emailAddress'];

	mysql_free_result($request);

	$request2 = db_query("SELECT memberName, emailAddress FROM {$db_prefix}members WHERE ID_MEMBER = $ID_MEMBER LIMIT 1", __FILE__, __LINE__);
	$row2 = mysql_fetch_assoc($request2);
	$sec_name = $row2['memberName'];
	$sec_email = $row2['emailAddress'];
	mysql_free_result($request2);

$m ="Hello $rec,

The following is an email sent to you by $sec_name via your account on $mbname. If this message is spam, contains abusive or other comments you find offensive please contact the webmaster of the board at the following address:

$webmaster_email

Message sent to you follows\n";
$m .=  strip_tags($message);
//For send mail function
require_once($sourcedir . '/Subs-Post.php');

		//Send email to member
		sendmail($rec_email, $subject, $m);

		//Check if it should send the sender a copy of email
		$sendcopy = $_POST['sendcopy'];
		if($sendcopy == 'ON')
		{
			sendmail($sec_email, $subject, $m);
		}

		//Show template that mail was sent
		loadtemplate('User_Email');

		//Load the main User Email template
		$context['sub_template']  = 'send';

		//Set the page title
		$context['page_title'] = $mbname . ' - Email sent.';

		}
	}
	else
	{
		$u = $_GET['u'];

		$request = db_query("SELECT memberName FROM {$db_prefix}members WHERE ID_MEMBER = $u LIMIT 1", __FILE__, __LINE__);
		$row = mysql_fetch_assoc($request);
		$context['user_email_name'] = $row['memberName'];
		$context['user_email_id'] = $u;

		//Load the main User Email template
		loadtemplate('User_Email');

		//Load the main User Email template
		$context['sub_template']  = 'main';

		//Set the page title
		$context['page_title'] = $mbname . ' - ' . $txt['user_email_title'];
	}
}
?>